<?php
class ServiceAdapterBase {
  protected $service_ = null;
  protected $protocol_ = null;
  protected $transport_ = null;

  function __construct($host, $port) {
    $this->InitThrift();
    $this->LoadIDL();
    $this->Connect($host, $port);
  }

  function __destruct() {
    $this->transport_->close();
  }


  private function InitThrift() {
    if (empty($GLOBALS["THRIFT_ROOT"])) {
      $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__) . '/thrift-0.8.0';
      $GLOBALS['THRIFT_GEN_ROOT'] = dirname(__FILE__) . '/gen-php';
      
      // Load up all the thrift stuff
      require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
      require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
      require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
      require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
      require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
    }
  }

  // 虚函数
  protected function LoadIDL() {}

  private function Connect($host, $port) {
    try {
      // Create a thrift connection (Boiler plate)
      $socket = new TSocket($host, $port);
      $socket->setSendTimeout(500);
      $socket->setRecvTimeout(500);
      $this->transport_= new TFramedTransport($socket);
      $this->protocol_ = new TBinaryProtocol($this->transport_);

      $this->transport_->open();
    } catch (TException $tx) {
      return -1;
    }
    return 0;
  }
}
?>
